package com.cloud.redis.service;

import com.cloud.redis.Entity.User;
import com.cloud.redis.controller.IpController;
import com.lwj.framework.constant.Constant;
import com.lwj.redislock.annotation.DistributedLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class TestService {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestService.class);
    /**
     * 模拟这个是商品库存
     */
    public static volatile Integer TOTAL = 10;

     @Cacheable(cacheManager = Constant.RedisCacheManager, cacheNames = Constant.ShortTimeCache)
     public User getUser(){
         User user=new User();
         user.setId(10L);
         user.setUsername("liwenjian");
         user.setPhone("111111");
        System.out.println("执行TestService方法");
         return user;
     }

    @DistributedLock("testLock")
    public String testLock(){
        if (TOTAL > 0) {
            TOTAL--;
            return "success";
        }
        LOGGER.info("===注解模式=== 减完库存后,当前库存===" + TOTAL);
        return "failure";
    }
}
